﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Entity.DataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.DataManage
{
    public class TaskRecMaintainService : DbHelperBase
    {
        public int GetTaskRecMaintainCount(PrdTaskQcEntity qcmodel)
        {
            var count = 0;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT COUNT(*) [count] FROM UT_TaskRec AS A   ");
            strSql.Append(" INNER JOIN UT_PrdTask AS B ON A.TaskID=B.TaskID ");
            strSql.Append(" INNER JOIN UT_Flow AS X ON B.FlowID=X.FlowID ");
            strSql.Append(" INNER JOIN UT_Product AS C ON X.PrdID=C.PrdID ");
            strSql.Append(" INNER JOIN UT_Shift AS D ON B.ShiftID=D.ShiftID ");
            strSql.Append(" INNER JOIN UT_Equipment AS E ON B.EqID=E.EqID ");
            strSql.Append(" INNER JOIN UT_Line AS F ON E.LineID=F.LineID ");
            strSql.Append(" INNER JOIN UT_User AS G ON A.UserID=G.UserID ");
            strSql.Append(" WHERE A.EndTime IS NULL ");
            if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
            {
                strSql.Append(" and B.BizDate>=@BizDateFrom");
            }
            if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
            {
                strSql.Append(" and B.BizDate<=@BizDateEnd");
            }
            if (!string.IsNullOrEmpty(qcmodel.EqID))
            {
                strSql.Append(" and B.EqID=@EqID ");
            }
            if (!string.IsNullOrEmpty(qcmodel.ShiftID))
            {
                strSql.Append(" and B.ShiftID=@ShiftID ");
            }
            if (!string.IsNullOrEmpty(qcmodel.PrdCode))
            {
                strSql.Append(" and C.PrdCode LIKE '%" + qcmodel.PrdCode + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" and F.LineID=@LineID ");
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
                    {
                        var date = Convert.ToDateTime(qcmodel.BizDateFrom);
                        Db.AddInParameter(cmd, "@BizDateFrom", date);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
                    {
                        var date = Convert.ToDateTime(qcmodel.BizDateEnd);
                        Db.AddInParameter(cmd, "@BizDateEnd", date);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.EqID))
                    {
                        Db.AddInParameter(cmd, "@EqID", qcmodel.EqID);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.ShiftID))
                    {
                        Db.AddInParameter(cmd, "@ShiftID", qcmodel.ShiftID);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.LineID))
                    {
                        Db.AddInParameter(cmd, "@LineID", qcmodel.LineID);
                    }
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        public IList<TaskRecMaintainEntity> GetTaskRecMaintainList(PrdTaskQcEntity qcmodel)
        {
            var list = new List<TaskRecMaintainEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by B.BizDate) AS idx,A.RecID,C.PrdCode,C.PrdName,B.BizDate,F.LineName,E.EqCode,E.EqName,D.ShiftName,B.PlanQty,B.ConfirmQty,B.PlanStartTime,B.PlanEndTime,A.StartTime,G.FullName FROM UT_TaskRec AS A   ");
            strSql.Append(" INNER JOIN UT_PrdTask AS B ON A.TaskID=B.TaskID ");
            strSql.Append(" INNER JOIN UT_Flow AS X ON B.FlowID=X.FlowID ");
            strSql.Append(" INNER JOIN UT_Product AS C ON X.PrdID=C.PrdID ");
            strSql.Append(" INNER JOIN UT_Shift AS D ON B.ShiftID=D.ShiftID ");
            strSql.Append(" INNER JOIN UT_Equipment AS E ON B.EqID=E.EqID ");
            strSql.Append(" INNER JOIN UT_Line AS F ON E.LineID=F.LineID ");
            strSql.Append(" INNER JOIN UT_User AS G ON A.UserID=G.UserID ");
            strSql.Append(" WHERE A.EndTime IS NULL ");
            if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
            {
                strSql.Append(" and B.BizDate>=@BizDateFrom");
            }
            if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
            {
                strSql.Append(" and B.BizDate<=@BizDateEnd");
            }
            if (!string.IsNullOrEmpty(qcmodel.EqID))
            {
                strSql.Append(" and B.EqID=@EqID ");
            }
            if (!string.IsNullOrEmpty(qcmodel.ShiftID))
            {
                strSql.Append(" and B.ShiftID=@ShiftID ");
            }
            if (!string.IsNullOrEmpty(qcmodel.PrdCode))
            {
                strSql.Append(" and C.PrdCode LIKE '%" + qcmodel.PrdCode + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" and F.LineID=@LineID ");
            }
            strSql.Append(" ) AS T ");
            strSql.Append(" Where T.idx between " + start + " and " + end);
            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {

                    if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
                    {
                        var date = Convert.ToDateTime(qcmodel.BizDateFrom);
                        Db.AddInParameter(cmd, "@BizDateFrom", date);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
                    {
                        var date = Convert.ToDateTime(qcmodel.BizDateEnd);
                        Db.AddInParameter(cmd, "@BizDateEnd", date);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.EqID))
                    {
                        Db.AddInParameter(cmd, "@EqID", qcmodel.EqID);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.ShiftID))
                    {
                        Db.AddInParameter(cmd, "@ShiftID", qcmodel.ShiftID);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.LineID))
                    {
                        Db.AddInParameter(cmd, "@LineID", qcmodel.LineID);
                    }
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new TaskRecMaintainEntity();
                            if (reader["RecID"] != DBNull.Value)
                                model.RecID = new Guid(reader["RecID"].ToString());
                            model.PrdCode = reader["PrdCode"] == obj ? null : Convert.ToString(reader["PrdCode"]);
                            model.PrdName = reader["PrdName"] == obj ? null : Convert.ToString(reader["PrdName"]);
                            if (reader["BizDate"] != DBNull.Value)
                                model.BizDate = Convert.ToDateTime(reader["BizDate"].ToString());
                            model.LineName = reader["LineName"] == obj ? null : Convert.ToString(reader["LineName"]);
                            model.EqCode = reader["EqCode"] == obj ? null : Convert.ToString(reader["EqCode"]);
                            model.EqName = reader["EqName"] == obj ? null : Convert.ToString(reader["EqName"]);
                            model.ShiftName = reader["ShiftName"] == obj ? null : Convert.ToString(reader["ShiftName"]);
                            if (reader["PlanQty"] != DBNull.Value)
                                model.PlanQty = Convert.ToInt32(reader["PlanQty"].ToString());
                            if (reader["ConfirmQty"] != DBNull.Value)
                                model.ConfirmQty = Convert.ToInt32(reader["ConfirmQty"].ToString());
                            if (reader["PlanStartTime"] != DBNull.Value)
                                model.PlanStartTime = Convert.ToDateTime(reader["PlanStartTime"].ToString());
                            if (reader["PlanEndTime"] != DBNull.Value)
                                model.PlanEndTime = Convert.ToDateTime(reader["PlanEndTime"].ToString());
                            if (reader["StartTime"] != DBNull.Value)
                                model.StartTime = Convert.ToDateTime(reader["StartTime"].ToString());
                            model.FullName = reader["FullName"] == obj ? null : Convert.ToString(reader["FullName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        public bool ModifyTaskRec(TaskRecMaintainEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE [UT_TaskRec] SET [EndTime] = @EndTime,[UserID] = @UserID WHERE [RecID] = @RecID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@EndTime", model.EndTime);
                    Db.AddInParameter(cmd, "@UserID", model.UserID);
                    Db.AddInParameter(cmd, "@RecID", model.RecID);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
    }
}
